package com.joseph.websocket.client;

import com.google.common.util.concurrent.*;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

public class DCLInstance {

    private static volatile DCLInstance INSTANCE = null;

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        ListenableFuture<Object> future = service.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                System.out.println("call in");
                TimeUnit.SECONDS.sleep(5);
                System.out.println("call out");
                return 10;
            }
        });

        //不阻塞当前线程，使用线程池获取结果，并调用回调函数
        Futures.addCallback(future, new FutureCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object result) {
                System.out.println("callback");
                System.out.println(result);
            }

            @Override
            public void onFailure(Throwable t) {

            }
        }, service);

        System.out.println("end");

        Thread.interrupted();

        new Thread().stop();

        Uninterruptibles.sleepUninterruptibly(20, TimeUnit.SECONDS);
        Futures.addCallback(future, new FutureCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object result) {
                System.out.println("callback2");
                System.out.println(result);
            }

            @Override
            public void onFailure(Throwable t) {

            }
        }, service);
    }
}
